package com.example.review;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * ClassName: ProducerPartitioner
 * Package: com.example.review
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-05
 * Time: 11:25
 */

//自定义分区器
public class ProducerPartitioner {
    public static void main(String[] args) {

        //0.创建配置信息
        Properties properties = new Properties();

        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092");

        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        //自定义分区器关联起来
        properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, MyPartitioner.class.getName());

        //com.example.review.MyPartitioner 就是获取全类名
        System.out.println(MyPartitioner.class.getName() + "JJJJJ");

        //1.创建生产者
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);

        //发送数据 如果这里指定看分区 那么这里指定的优先级高于 自定义的
        kafkaProducer.send(new ProducerRecord<>("first", 1, "", "atguigu"), new Callback() {
            @Override
            public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                System.out.println(recordMetadata.topic() + " " + recordMetadata.partition());
            }
        });

        //关闭资源

        kafkaProducer.close();


    }
}
